package cn.flyme.shop.entities;

import java.util.List;

public class Page {
    private int curPage;//当前页               *
    private int pageSize = 5;//每页显示的条数          *
    private int totalRecords;//总记录条数                *
    private int startIndex;//查询的开始记录索引          *
    private int totalPage;//总页数                 *
    private int prePage;//上一页                   *
    private int nextPage;//下一页                  *
    private List<Goods> records;//分好页的结果集（最多只能有5个对象）

    //用于显示页码的属性。我们的需求就是页面上最多只显示5个页码。当前页在允许的情况下永远居中
    private int beginPage;
    private int endPage;

    /**
     * 要想使用此类，必须提供2个参数
     * @param curPage  当前页
     * @param totalRecords      总记录条数
     */
    public Page(int curPage, int totalRecords){
        this.curPage= curPage;
        this.totalRecords = totalRecords;

        //计算开始记录索引
        startIndex = (curPage - 1) * pageSize;

        //计算总页数
        totalPage = totalRecords%pageSize==0 ? totalRecords/pageSize : totalRecords/pageSize+1;

        //计算页号
        if(totalPage < 6){
            beginPage = 1;
            endPage = totalPage;
        }else{
            beginPage = curPage - 2;
            endPage= curPage + 2;
            if(beginPage < 1){
                beginPage = 1;
                endPage = beginPage + 5;
            }
            if(endPage > totalPage){
                endPage = totalPage;
                beginPage = endPage - 5;
            }
        }
    }

    public int getPrePage() {
        //计算上一页
        prePage = curPage - 1;
        if(prePage < 1){
            prePage = 1;
        }
        return prePage;
    }

    public int getNextPage() {
        //计算下一页
        nextPage = curPage+ 1;
        if(nextPage > totalPage){
            nextPage = totalPage;
        }
        return nextPage;
    }

    public int getCurPage() {
        return curPage;
    }

    public void setCurPage(int curPage) {
        this.curPage = curPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotalRecords() {
        return totalRecords;
    }

    public void setTotalRecords(int totalRecords) {
        this.totalRecords = totalRecords;
    }

    public int getStartIndex() {
        return startIndex;
    }

    public void setStartIndex(int startIndex) {
        this.startIndex = startIndex;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public List<Goods> getRecords() {
        return records;
    }

    public void setRecords(List<Goods> records) {
        this.records = records;
    }

    public int getBeginPage() {
        return beginPage;
    }

    public void setBeginPage(int beginPage) {
        this.beginPage = beginPage;
    }

    public int getEndPage() {
        return endPage;
    }

    public void setEndPage(int endPage) {
        this.endPage = endPage;
    }

    public void setPrePage(int prePage) {
        this.prePage = prePage;
    }

    public void setNextPage(int nextPage) {
        this.nextPage = nextPage;
    }

}
